from system.object import Object
from system.gameServer import gameServer
from system.eventServer import eventServer
from system.timer import Timer

import random

class Shudai(Object):
    def __init__(self, parent, type = None, name = None):
        Object.__init__(self,parent,type,name)
        
        self.unitCount = 0
                
        self.defaultVar("resources",["oil","power"])
        
        self.camera = self.addChild("camera")
        self.camera.setVar("renderOp2d","camera")
        self.camera.setVar("pos",(0,0))
        self.camera.setVar("scale",(1,1))
        
        self.bullets = self.camera.addChild("bullets")
        self.units = self.camera.addChild("units")
        
        self.player = self.addChild("player")
        self.player.setVar("side","blue")
        for resource in self.getVar("resources"):
            defaultName = "default_%s" % resource
            if self.hasVar(defaultName):
                default = self.getVar(defaultName)
            else:
                default = 100
            self.player.setVar(resource,default)
        
        eventServer.register("createUnit",self.eventCreateUnit)
        eventServer.register("setSide",self.setSide)
        eventServer.register("reloadMap",self.reloadMap)
        eventServer.register("reset",self.reset)
        
        self.defaultVar("map","")
        self.loadMap(self.getVar("map"))
        
        self.interfaces = self.addChild("interfaces")
        if gameServer.interfaces.__class__ == dict:
            for name, vars in gameServer.interfaces.iteritems():
                if vars.has_key("type"):
                    type = vars['type']
                else:
                    type = name.split('_')[0]
                    
                interface = self.interfaces.addChild(type,name)
                interface.vars.update(vars)
                
                print "loaded interface", name
                
        #self.gaTest = self.addChild("shudai.gaTest")
                
    def reset(self, cmd, args):
        self.units.clearChildren()
        self.bullets.clearChildren()
        self.removeChild("map")
        self.camera.setVar("pos",(0,0))
        self.camera.setVar("scale",(1,1))
        
    def reloadMap(self, cmd, args):
        self.reset(cmd,args)
        self.loadMap(args['map'])
        
    def loadMap(self, mapName):
        self.log("load map %s" % mapName)
        self.addChildData({"include":[mapName]})
        self.map = self.addChild("shudai.map","map")
        
    def setSide(self, cmd, args):
        self.log("set side %s" % args['side'])
        self.player.setVar("side",args['side'])
        
    def eventCreateUnit(self, cmd, args):
        unit = self.createUnit(args['type'],args)
        
        msgArgs = {'path':unit.getVar("path")}
        msgArgs.update(args)
        eventServer.pull('unitCreated',msgArgs)
        
    def createUnit(self, type, args = None):
        unit = self.units.addChild("shudai.unit","unit%d" % self.unitCount)
        self.unitCount += 1
        unit.load(type)
        if args:
            unit.vars.update(args)
            unit.setVar("type","shudai.unit")
        unit.init()
        if args and args.has_key("side"):
            side = args['side']
        else:
            side = self.player.getVar("side")
        unit.setSide(side)
        return unit
